package com.layer8apps;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.*;
import android.widget.*;
import com.layer8apps.scanner.IntentIntegrator;
import com.layer8apps.scanner.IntentResult;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends Activity {

    private final int PRODUCT_CHOICE = 1000;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loadProducts();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        SearchView search = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_scan:
                createScanner();
                /*Intent prodint = new Intent(this, MooDBSearch.class);
                Bundle data = new Bundle();
                data.putString("product", "2519204988");
                prodint.putExtras(data);
                startActivityForResult(prodint, PRODUCT_CHOICE);*/
                return true;
            default:
                return onOptionsItemSelected(item);
        }
    }

    private void createScanner() {
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.initiateScan();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanResult != null) {
            String scan = scanResult.getContents();
            if (scan != null) {
                if (scan.length() >= 10) {
                    if (scan.length() > 10) {
                        scan = scan.substring(1,11);
                    }
                    Intent prodint = new Intent(this, MooDBSearch.class);
                    Bundle data = new Bundle();
                    data.putString("product", scan);
                    prodint.putExtras(data);
                    startActivityForResult(prodint, PRODUCT_CHOICE);
                }
            }
        } else{
            if (requestCode == PRODUCT_CHOICE && resultCode == RESULT_OK) {
                loadProducts();
            }
        }
    }

    private void loadProducts() {
        DatabaseHandler db = new DatabaseHandler(this);
        ArrayList<Product> productList = db.getAllProducts();
        if (productList != null) {
            if (productList.size() > 0) {
                ListView lv = (ListView) findViewById(R.id.productList);
                ProductListAdapter productListAdapter = new ProductListAdapter(getBaseContext(), productList);
                lv.setAdapter(productListAdapter);
            } else {
                Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private class ProductListAdapter extends BaseAdapter {

        ArrayList<Product> productList;
        private LayoutInflater mInflater;

        public ProductListAdapter(Context mContext, ArrayList<Product> productList) {
            mInflater = LayoutInflater.from(mContext);
            this.productList = productList;
        }

        @Override
        public int getCount() {
            return productList.size();
        }

        @Override
        public Object getItem(int i) {
            return productList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            final ViewHolder holder;
            if (view == null) {
                view = mInflater.inflate(R.layout.listitem, null);
                holder = new ViewHolder();
                holder.imgCase = (ImageView) view.findViewById(R.id.listImage);
                holder.listLayout = (LinearLayout) view.findViewById(R.id.listLayout);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            File dir = new File(Environment.getExternalStorageDirectory() + "/MovieKeeper/");
            Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" + productList.get(i).getImage() + ".png");
            holder.imgCase.setImageBitmap(bmp);
            holder.listLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                File sDir = new File(Environment.getExternalStorageDirectory() + "/MovieKeeper/" + productList.get(i).getImage() + ".png");
                sDir.delete();
                DatabaseHandler db = new DatabaseHandler(getBaseContext());
                db.deleteProduct(productList.get(i));
                loadProducts();
                holder.listLayout.removeAllViews();
                }
            });
            return view;
        }

    }

    static class ViewHolder {
        ImageView imgCase;
        LinearLayout listLayout;
    }

}
